﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Window;

namespace WmsApp
{
    public partial class FormWelcome : BaseForm
    {
        private const string MsgTextVersion = "程序集版本：{0}";
        private const string MsgTextFile = "文件{0}版本：{1}\r\n";

        public FormWelcome()
        {
            InitializeComponent();
            RegisterEvent<FormBorrowRepairQuery>(this.CmdBorrowRepair);
            RegisterEvent<FormStoreQuery>(this.CmdStoreQuery);
            RegisterEvent<FormPayApplyBill>(this.CmdPayApplyBill);
            RegisterEvent<FormInvoice>(this.CmdInvoice);
            RegisterEvent<FormIn>(this.CmdInStore);
            RegisterEvent<FormOut>(this.CmdOutStore);
            RegisterEvent<FormInInvoice>(this.CmdInvoiceInStore);
            RegisterEvent<FormStockTacking>(this.CmdStockTacking);

            this.labelVersion.Text = String.Format(MsgTextVersion, this.GetAsseblyVersion());
            IFileInfoLoader loader = new FileInfoLoader();
            this.labelFile.Text = loader.GetFileInfoListString();
        }

        private class FileInfo
        {
            public string FileName { get; set; }
            public string Version { get; set; }
        }

        private interface IFileInfoLoader
        {
            string GetFileInfoListString();
        }

        private class FileInfoLoader : IFileInfoLoader
        {
            private string fileInfoListString = "";
            public FileInfoLoader()
            {
                string path = Application.StartupPath + "\\localfile";
                if (Directory.Exists(path))
                {
                    String[] files = Directory.GetFiles(path);
                    var list = new List<FileInfo>();
                    foreach (var file in files)
                    {
                        try
                        {
                            string t = File.ReadAllText(file);
                            var fileInfo = new FileInfo();
                            fileInfo.Version = t;
                            string fn = file.Substring(file.LastIndexOf('\\') + 1);
                            fn = fn.Substring(0, fn.LastIndexOf(".") );
                            fileInfo.FileName = fn;
                            list.Add(fileInfo);
                        }
                        catch (Exception exception)
                        {
                            Exceptions.ExceptionHandler.ShowException(exception);
                        }
                    }

                    foreach (var fileInfo in list)
                    {
                        this.fileInfoListString += (String.Format(MsgTextFile,fileInfo.FileName,fileInfo.Version) );
                    }
                }
            }

            public string GetFileInfoListString()
            {
                return fileInfoListString;   
            }
        }



        private static void RegisterEvent<TF>(SimpleButton simpleButton) where TF : BaseForm, new()
        {
            simpleButton.Click +=
                (object sender, EventArgs e) =>
                {
                    var button = (SimpleButton)sender;
                    FormMain.FormMainInstance.ShowChildrenForm<TF>();
                };

        }

        public override string HeadText
        {
            get
            {
                return "欢迎来到Wms管理系统";
            }
        }
    }
}